#ifndef __TUNER_MLB_STUFF
#define __TUNER_MLB_STUFF

/*
 *   This file is part of ed_pvr.
 *
 *   ed_pvr is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   ed_pvr is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with ed_pvr.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <curl/curl.h>
#include "device.h"
#include "hls.h"
#include "win.h"


#define TUNER_MLB_MAX_STR_LEN					4096
#define TUNER_MLB_MAX_GAMES						30
#define TUNER_MLB_MAX_GAMES_INNINGS				30
#define TUNER_MLB_MAX_EVENTS					HLS_MAX_STREAMS
#define TUNER_MLB_EVENTID_MAX_SLEN				32

#define TUNER_MLB_MAX_EPG_CACHE_DAYS			30
//#define TUNER_MLB_MAX_EPG_CACHE_EXPIRE			120 // in seconds
#define TUNER_MLB_MAX_EPG_CACHE_EXPIRE			(24 * 60 * 60) // in seconds

#define TUNER_MLB_PARAM_START_BITRATE			"sbitrate"
#define TUNER_MLB_PARAM_MIN_BITRATE				"minbitrate"
#define TUNER_MLB_PARAM_MAX_BITRATE				"maxbitrate"
#define TUNER_MLB_PARAM_GAME_DATETIME			"gamedatetime"
#define TUNER_MLB_PARAM_ADAPTIVE_BITRATE		"adaptive"
#define TUNER_MLB_PARAM_GAME_STARTTIME			"gamestarttime"
#define TUNER_MLB_PARAM_AUTH_PROXY				"aproxy"
#define TUNER_MLB_PARAM_FETCH_PROXY				"fproxy"
#define TUNER_MLB_PARAM_FORCE_CDN				"cdn"


struct tuner_mlb_epg_xml_cache
{
	uint64_t time;
	char *url;
	int month;
	int day;
	int year;
	UTILS_DATAPAIR_STRUCT *epg;
	UTILS_DATAPAIR_STRUCT *grid;
};
typedef struct tuner_mlb_epg_xml_cache TUNER_MLB_EPG_XML_CACHE;


struct tuner_mlb_inning_struct
{
//	uint8_t inning;
	uint64_t top_start_time;
	uint64_t top_end_time;
	uint64_t bottom_start_time;
	uint64_t bottom_end_time;
};
typedef struct tuner_mlb_inning_struct TUNER_MLB_INNING_STRUCT;

struct tuner_mlb_innings_struct
{
	char					innings_url[TUNER_MLB_MAX_STR_LEN];

	char 					start_str[TUNER_MLB_MAX_STR_LEN];
	uint64_t 				start_time;

	char 					stream_start_str[TUNER_MLB_MAX_STR_LEN];
	uint64_t 				stream_start_time;

	TUNER_MLB_INNING_STRUCT innings[TUNER_MLB_MAX_GAMES_INNINGS];
};
typedef struct tuner_mlb_innings_struct TUNER_MLB_INNINGS_STRUCT;

struct tuner_mlb_b64url_cache
{
	char		innings_url[TUNER_MLB_MAX_STR_LEN];
//	char 		session_key[TUNER_MLB_MAX_STR_LEN];
	uint8_t 	b64_url[TUNER_MLB_MAX_STR_LEN];
	char 		calendar_event_id[24];
	char 		id[32];
	uint64_t 	last_update;
//	char		start_str[TUNER_MLB_MAX_STR_LEN];
	struct tuner_mlb_b64url_cache *next;
};
typedef struct tuner_mlb_b64url_cache TUNER_MLB_B64URL_CACHE;


struct tuner_mlb_game_event_id_struct
{
	uint8_t 			game_number;
	uint8_t 			b64_url[TUNER_MLB_MAX_STR_LEN];
	char 				type[TUNER_MLB_EVENTID_MAX_SLEN];
	char 				id[TUNER_MLB_EVENTID_MAX_SLEN];
	char 				display[TUNER_MLB_EVENTID_MAX_SLEN];
	char 				free[TUNER_MLB_EVENTID_MAX_SLEN];
	char 				combined_media_state[TUNER_MLB_EVENTID_MAX_SLEN];
	char 				state[TUNER_MLB_EVENTID_MAX_SLEN];
	char 				blackout[TUNER_MLB_EVENTID_MAX_SLEN];
	char 				playback_scenario[TUNER_MLB_EVENTID_MAX_SLEN];

	HLS_MASTER_URL 		*hls;
	uint8_t 			aes_key[AES128_KEY_SIZE];
	void 				*extra_data;

	struct tuner_mlb_game_struct *parent;
	struct tuner_mlb_game_event_id_struct *next;
};
typedef struct tuner_mlb_game_event_id_struct TUNER_MLB_EVENTID_STRUCT;


struct tuner_mlb_game_struct
{
	char 		calendar_event_id[24];
	uint64_t 	start_epoch;
	char 		id[32];
	char 		status[32];

	char		venue[120];
	char		game_data_directory[4096];

	char		home_team_city[32];
	char		home_team_name[64];
	char		home_team_code[6];
	char		home_team_file_code[6];

	char		away_team_city[32];
	char		away_team_name[64];
	char		away_team_code[6];
	char		away_team_file_code[6];

	TUNER_MLB_INNINGS_STRUCT 	innings;
	TUNER_MLB_EVENTID_STRUCT	events[TUNER_MLB_MAX_EVENTS];
	uint8_t						events_count;

	char		status_msg[TUNER_MLB_MAX_STR_LEN*4];
};
typedef struct tuner_mlb_game_struct TUNER_MLB_GAME_STRUCT;


struct tuner_mlb_auth_struct
{
	char ipid[TUNER_MLB_MAX_STR_LEN];
	char fprt[TUNER_MLB_MAX_STR_LEN];
	char session_key[TUNER_MLB_MAX_STR_LEN];
	char blackout[TUNER_MLB_MAX_STR_LEN];

	time_t ipid_expire;
	time_t fprt_expire;
	time_t current_time;
};
typedef struct tuner_mlb_auth_struct TUNER_MLB_AUTH_STRUCT;

struct tuner_mlb_struct
{
	DEVICE_STRUCT *device;
	TUNER_MLB_GAME_STRUCT games[TUNER_MLB_MAX_GAMES];
	uint8_t last_stream_index;
	uint8_t games_count;
	uint8_t games_total_count;
	uint64_t games_last_mod_time;
	uint64_t games_fetch_time;

	uint8_t device_count;
	int8_t device_num[16];
	char *config_file;
	char *channels_file;
	char *skey_file;
	char *dev_path_str;
	uint32_t channel_num_offset;

	char *login;
	char *password;
	char *cookiefile;

	int connect_timeout;
	int fetch_timeout;

	uint8_t force_cdn;

	uint8_t auth_proxy_type;
	char auth_proxy[TUNER_MLB_MAX_STR_LEN];
	uint8_t fetch_proxy_type;
	char fetch_proxy[TUNER_MLB_MAX_STR_LEN];

	uint8_t *read_overflow;
	uint32_t read_overflow_fill_pos;
	uint32_t read_overflow_read_pos;
	uint32_t read_overflow_sz;

	uint32_t start_bitrate;
	uint32_t min_bitrate;
	uint32_t max_bitrate;
	uint64_t game_datetime;
	uint8_t adaptive;
	uint64_t game_starttime;
	uint32_t epg_days;

//	uint64_t need_sync_tc;
	double need_sync_tc;


	double playlist_stream_offset;
	double playlist_time;
	uint64_t pts_time;
	uint64_t last_auth_time;
	uint32_t reauth_time;
	uint32_t fetch_lag_count;

	HLS_FETCH_OPTIONS fetch_opts;
	TUNER_MLB_AUTH_STRUCT auth;

	TUNER_MLB_B64URL_CACHE *first_b64cache;
	TUNER_MLB_B64URL_CACHE *current_b64cache;


	TUNER_MLB_EVENTID_STRUCT *live_event_struct;

	TUNER_MLB_EPG_XML_CACHE epg_cache[TUNER_MLB_MAX_EPG_CACHE_DAYS];

	CURL *curl_handle;
	pthread_mutex_t curl_mutex;
	pthread_mutex_t init_mutex;
	pthread_mutex_t seek_mutex;
	NCURSES_SCREEN *screen;

};
typedef struct tuner_mlb_struct TUNER_MLB_STRUCT;


TUNER_MLB_STRUCT * tuner_mlb_init(DEVICE_INIT_ARGS_STRUCT *);

#ifdef __cplusplus
    }
#endif


#endif
